package com.hkzhao.shixun.dto;

import com.hkzhao.shixun.mbg.model.Coreuser;
import com.hkzhao.shixun.mbg.model.Userpermission;
import com.hkzhao.shixun.service.CoreUserSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <h3>j2ee</h3>
 * <p>xwo用户和管理员登录的细节实现</p>
 *
 * @author : hkzhao
 * @date : 2020-06-05 12:52
 **/
public class CoreUserDetail implements UserDetails {
    private Coreuser coreuser;
    private String password;
    private List<Userpermission> permissionList;
    public CoreUserDetail(Coreuser coreuser, List<Userpermission> permissions, String password){
        this.coreuser = coreuser;
        this.permissionList = permissions;
        this.password = password;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return permissionList.stream().filter(userPermission -> userPermission.getValue()!=null)
                .map(userPermission -> new SimpleGrantedAuthority(userPermission.getValue()))
                .collect(Collectors.toList());
    }
    @Override
    public String getPassword(){
        return password;
    }
    @Override
    public String getUsername(){
        return coreuser.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }
}